#include"kernel/fcntl.h"
#include"kernel/types.h"
#include"user/user.h"
int main(){
    int fd[2],fdc[2];
    const char buf[2][0x10]={"ping","pong"};
    char p_buf[10],c_buf[10];
    int fpid=0;
    int p,c;
    pipe(fd);
    pipe(fdc);
    fpid=fork();
    if(fpid<0){
        write(2,"fork error",11);
        exit(1);
    }
    else if(fpid==0){
        //child process
        close(fd[1]);
        printf("%d received ",getpid());
        c=read(fd[0],c_buf,5);
        write(2,c_buf,5);
        close(fd[0]);

        close(fdc[0]);
        
        write(fdc[1],buf[1],c);
        printf("\n");
        close(fdc[1]);
        exit(0);
    }
    else {
        close(fd[0]);
        write(fd[1],buf[0],5);
        close(fd[1]);
        wait(0);

        close(fdc[1]);
        p=read(fdc[0],p_buf,5);
        printf("%d received ",getpid());
        write(2,p_buf,p);
        printf("\n");
        close(fdc[0]);
        exit(0);

    }
}